************This do file gathers all the necessary variable needed to estimate the impact of the war on drugs in growth for States in Mexico***********

clear
set more off
set mem 2000m
local pathdata "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Data\Final"
cd "`pathdata'"
 

*cd   "C:\Users\Lauren Velasco\Dropbox\1.3 Research\general_mex\Paper 28 Accidents\analysis\data\"
use Base.dta, clear

local pathdata2 "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Paper Oct 23\output"
cd "`pathdata2'"
rename ano yr

drop if yr == 2015 | yr == 2016

 * S1 Generate variable COVID
 gen Treatment = yr==2020 | yr==2021 | yr==2022
 gen Post=mes>=3 & mes<=12
 gen COVID=Treatment*Post
 replace COVID=1 if (yr==2021 & mes<=3) |  (yr==2022 & mes<=3) /*Change to 2020 */
 tab COVID
 tab mes yr if COVID==1
 label var COVID "1(COVID-19)" 
 
 g Robberyt = RoboAutopartesCont + RoboTrasnportistaCont + RoboTranseunteVPCont + RoboTranseunteEACont + RoboTransportePubIndCont  /*
 */+ RoboTransportePubColCont + RoboTransporteIndividualCont + RoboBancoCont + RoboNegocioCont + RoboGanadoCont + RoboMaquinariaCont + RoboOtroCont
 g Theftt = RoboAutopartesSint + RoboTrasnportistaSint + RoboTranseunteVPSint + RoboTranseunteEASint + RoboTransportePubIndSint + RoboTransportePubColSint /*
 */ + RoboTransporteIndividualSint + RoboBancoSint + RoboNegocioSint + RoboGanadoSint + RoboMaquinariaSint + RoboOtroSint
 g Propertyt = DanoPropt+ Despojot
 *********************************** 
 * Table 2: Dif-in-Dif
 *********************************** 
/* 
 global dep  Robot RoboCasat RoboVeht RoboTranseuntet RoboBancot RoboNegociot Fraudet Falsificaciont 

 
	 foreach var in $dep {
		eststo `var': reghdfe  `var' COVID i.mes i.yr [aw=pob], absorb( i.Clave) cluster(Clave)
		estadd local fe "X" : `var'  
		foreach i in 0 1 {
			sum  `var'  [aw=pob] if COVID==`i'  & Treatment==1
			local  m`i' : di %9.2fc r(mean)  
			estadd local m`i'   "`m`i''" : `var' 
		}
		display _b[COVID]
		local b=_b[COVID]
		local pch=(`b'  /`m0')*100
		local per : di %9.1fc `pch'
		estadd local per   "`per'\%" : `var' 
	 }
	  
	global label "\shortstack{Rates Per\\100,000 Inh.:}"
	global mgroups " "\shortstack{Total\\Theft}"  "\shortstack{Trass-\\passing}" "\shortstack{Car\\Theft}" "\shortstack{Pass-\\esby\\Theft}" "\shortstack{Bank\\Robbery}" "\shortstack{Shopli-\\fting}" "\shortstack{Fraud}" "\shortstack{ID\\Theft}""  
	global pattern "1 1 1 1 1 1 1 1 1"
	esttab Robot RoboCasat RoboVeht RoboTranseuntet RoboBancot RoboNegociot Fraudet Falsificaciont using "Table_Dif1.tex" , replace  se  /*
					*/ noconstant  lines 	f	compress    /*
					*/ mlabels(none)   	 /*   
					*/ label 	booktabs  b(%9.3fc) se(%9.3fc) /*
					*/ alignment(C C)   star(* 0.10 ** 0.05 *** 0.01) /*
					*/ keep( *COVID* )     /*
					*/ nobaselevels   /*
					*/ stats(N r2_a m0 g per k  fe        ,  /*
					*/ labels("N" "Adjusted R-squared"     "Mean Treatment Pre-COVID" "  \midrule \vspace{-4mm}" /*
					*/ "COVID-19 Percentage Change" "\hline \vspace{-3mm}" "Baseline FE"    )  /*
					*/	 fmt(%9.0fc %9.3fc %9.3fc  %9.3fc  %9.3fc %9.3fc )  )    /*
					*/ mgroups( $mgroups , lhs("\textsc{${label}}") /*
					*/ pattern( $pattern ) span  /*
					*/ prefix(\multicolumn{@span}{c}{) suffix(})   erepeat(\cmidrule(lr){@span}))

		*/			 
					 
  *********************************** 
 * Table 3: Dif-in-Dif by road user
 *********************************** 
/*  global dep2 condherido_rate condmuerto_rate pasaherido_rate pasamuerto_rate peatherido_rate peatmuerto_rate othrherido_rate othrmuerto_rate
 
 
	 foreach var in $dep2 {
		eststo `var': reghdfe  `var' COVID i.mes i.yr [aw=pob], absorb( i.ent_num) cluster(ent_num)
		estadd local fe "X" : `var'  
		foreach i in 0 1 {
		    sum  `var'  [aw=pob] if COVID==`i'  & Treatment==1
			local  m`i' : di %9.2fc r(mean)  
			estadd local m`i'   "`m`i''" : `var' 
		}
		display _b[COVID]
		local b=_b[COVID]		
		local pch=(`b'  /`m0')*100
		local per : di %9.1fc `pch'
		estadd local per   "`per'\%" : `var' 
	 }
	  
	global label "\shortstack{Rates Per\\100,000 Inh.:\\by Road User:}"
	global mgroups " "\shortstack{Driver\\Injury}" "\shortstack{Driver\\Fata-\\lity}"  "\shortstack{Pass-\\enger-\\Injury}" "\shortstack{Pass-\\enger-\\Fata-\\lity}" "\shortstack{Pedes-\\trian-\\Injury}" "\shortstack{Pedes-\\trian-\\Fata-\\lity}" "\shortstack{Other\\User\\Injury}" "\shortstack{Other\\Users\\Fata-\\lity}" "  
	global pattern "1 1 1 1 1 1 1 1 1"
	esttab condherido_rate condmuerto_rate pasaherido_rate pasamuerto_rate peatherido_rate peatmuerto_rate othrherido_rate othrmuerto_rate using "Table_Dif2.tex" , replace  se  /*
					*/ noconstant  lines 	f	compress    /*
					*/ mlabels(none)   	 /*   
					*/ label 	booktabs  b(%9.3fc) se(%9.3fc) /*
					*/ alignment(C C)   star(* 0.10 ** 0.05 *** 0.01) /*
					*/ keep( *COVID* )     /*
					*/ nobaselevels   /*
					*/ stats(N r2_a m0 g per k  fe        ,  /*
					*/ labels("N" "Adjusted R-squared"     "Mean Treatment Pre-COVID" "  \midrule \vspace{-4mm}" /*
					*/ "COVID-19 Percentage Change" "\hline \vspace{-3mm}" "Baseline FE"    )  /*
					*/	 fmt(%9.0fc %9.3fc %9.3fc  %9.3fc  %9.3fc %9.3fc )  )    /*
					*/ mgroups( $mgroups , lhs("\textsc{${label}}") /*
					*/ pattern( $pattern ) span  /*
					*/ prefix(\multicolumn{@span}{c}{) suffix(})   erepeat(\cmidrule(lr){@span}))
*/					
					
  *********************************** 
 * Table 4: Table Mechanisms
 ***********************************
/*					
  global dep3 edad men speed traffic distract drunk belt escape
 
 
	 foreach var in $dep3 {
		eststo `var': reghdfe  `var' COVID i.mes i.yr [aw=pob], absorb( i.ent_num) cluster(ent_num)
		estadd local fe "X" : `var'  
		foreach i in 0 1 {
		    sum  `var'  [aw=pob] if COVID==`i'  & Treatment==1
			local  m`i' : di %9.2fc r(mean)  
			estadd local m`i'   "`m`i''" : `var' 
		}
		display _b[COVID]
		local b=_b[COVID]		
		local pch=(`b'  /`m0')*100
		local per : di %9.1fc `pch'
		estadd local per   "`per'\%" : `var' 
	 }
	  
	global label "\shortstack{Rates Per\\100,000 Inh.:}"
	global mgroups " "\shortstack{Driver\\Age}"  "\shortstack{Driver\\Sex\\(man=1)}" "\shortstack{Speed-\\ing}"  "\shortstack{Traf-\\fic}" "\shortstack{Distr-\\action}" "\shortstack{DUI}" "\shortstack{Seat-\\belt Off}" "\shortstack{Driver\\Escape}" "  
	global pattern "1 1 1 1 1 1 1 1 1"
	esttab edad men speed traffic distract drunk belt escape using "Table_Dif3.tex" , replace  se  /*
					*/ noconstant  lines 	f	compress    /*
					*/ mlabels(none)   	 /*   
					*/ label 	booktabs  b(%9.3fc) se(%9.3fc) /*
					*/ alignment(C C)   star(* 0.10 ** 0.05 *** 0.01) /*
					*/ keep( *COVID* )     /*
					*/ nobaselevels   /*
					*/ stats(N r2_a m0 g per k  fe        ,  /*
					*/ labels("N" "Adjusted R-squared"     "Mean Treatment Pre-COVID" "  \midrule \vspace{-4mm}" /*
					*/ "COVID-19 Percentage Change" "\hline \vspace{-3mm}" "Baseline FE"    )  /*
					*/	 fmt(%9.0fc %9.3fc %9.3fc  %9.3fc  %9.3fc %9.3fc )  )    /*
					*/ mgroups( $mgroups , lhs("\textsc{${label}}") /*
					*/ pattern( $pattern ) span  /*
					*/ prefix(\multicolumn{@span}{c}{) suffix(})   erepeat(\cmidrule(lr){@span}))
							
 */
  ***********************************
 * Figure 1-4. Event Study           *
 ***********************************
 
 
 gen event=.

 *replace event=-14 if mes==1 & yr==2019 /*Change to 2019 */
 *replace event=-13 if mes==2 & yr==2019
 *replace event=-12 if mes==3 & yr==2019
 *replace event=-11 if mes==4 & yr==2019
 *replace event=-10 if mes==5 & yr==2019
 replace event=-9  if mes==6 & yr==2019
 replace event=-8 if mes==7 & yr==2019
 replace event=-7 if mes==8 & yr==2019
 replace event=-6 if mes==9 & yr==2019
 replace event=-5 if mes==10 & yr==2019
 replace event=-4 if mes==11 & yr==2019
 replace event=-3 if mes==12 & yr==2019
  replace event=-2 if mes==1 & yr==2020 /*Change to 2020 */
 replace event=-1 if mes==2 & yr==2020
 replace event=0 if mes==3 & yr==2020
 replace event=1 if mes==4 & yr==2020
 replace event=2 if mes==5 & yr==2020
 replace event=3 if mes==6 & yr==2020
 replace event=4 if mes==7 & yr==2020
 replace event=5 if mes==8 & yr==2020
 replace event=6 if mes==9 & yr==2020
 replace event=7 if mes==10 & yr==2020
 replace event=8 if mes==11 & yr==2020
 replace event=9 if mes==12 & yr==2020
 replace event=10 if mes==1 & yr==2021
 replace event=11 if mes==2 & yr==2021
 replace event=12 if mes==3 & yr==2021
 replace event=13 if mes==4 & yr==2021
 replace event=14 if mes==5 & yr==2021
 replace event=15 if mes==6 & yr==2021
 replace event=16 if mes==7 & yr==2021
 replace event=17 if mes==8 & yr==2021
 replace event=18 if mes==9 & yr==2021
 replace event=19 if mes==10 & yr==2021
 replace event=20 if mes==11 & yr==2021
 replace event=21 if mes==12 & yr==2021
 replace event=22 if mes==1 & yr==2022
 replace event=23 if mes==2 & yr==2022
 replace event=24 if mes==3 & yr==2022
 replace event=25 if mes==4 & yr==2022
 replace event=26 if mes==5 & yr==2022
 replace event=27 if mes==6 & yr==2022
 replace event=28 if mes==7 & yr==2022
 replace event=29 if mes==8 & yr==2022
 replace event=30 if mes==9 & yr==2022
 replace event=31 if mes==10 & yr==2022
 replace event=32 if mes==11 & yr==2022
 replace event=33 if mes==12 & yr==2022
 
 recode event (.=-1) /*year 2018 and 2017*/
 
 tab event
 tab mes yr if event>=0
 tab mes yr if event<0
 tab mes yr if event==-1
 
 /*
 gen eventt=even /* Esta es la que se puede manipular.Escojo el {max, min} de tab even */
 replace eventt=-9 if event<-9
 replace eventt=9 if event>9
 tab eventt
 */
 *Figure 1a
 

 grstyle init
 grstyle set lpattern dot  solid     /*patron de las dos lineas */
 grstyle set linewidth    medthick  thick /*Grosor de las dos lineas */
 grstyle set plain  , horizontal compact  minor dotted
 grstyle set color sienna black 
 grstyle set symbol 
 
 *sum total_acci_rate muertos_tot_rate her_tot_rate fatal condmuerto_rate condherido_rate pasamuerto_rate pasaherido_rate peatmuerto_rate peatherido_rate othrmuerto othrherido
 *local dep total_acci_rate muertos_tot_rate her_tot_rate fatal condmuerto_rate condherido_rate pasamuerto_rate pasaherido_rate peatmuerto_rate peatherido_rate othrmuerto othrherido_rate
 *global dep4  muertos_tot_rate her_tot_rate
 
 *replace yr = 2019 if yr==2019
 replace yr = 2020 if yr==2021 | yr==2022
 
 	 foreach v of varlist Fraudet {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(-3(1)3 , angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Frauds per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Frauds", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(2.5 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(2.5 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 
	 graph export "Figure2.png", as(png) width(1000) replace

	 
	  	 foreach v of varlist  Robberyt {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Robberies per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Robberies", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(-1 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(-1 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 graph save Robberyt, replace
	 graph export "Figure3.png", as(png) width(1000) replace
	 
	 
	 	  	 foreach v of varlist  Theftt {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Thefts per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Thefts", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(-.75 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(-.75 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 	 graph save Theftt, replace
	 graph export "Figure4.png", as(png) width(1000) replace
	 
	 
	 	 	  	 foreach v of varlist  RoboCasat {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Domestic Burglaries per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Domestic Burglaries", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(.5 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(.5 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 	 graph save RoboCasat, replace
	 graph export "Figure5.png", as(png) width(1000) replace
	 
	 
	 
	 	 	 	  	 foreach v of varlist  RoboVeht{
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "MVT per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Motor-Vehicle Thefts", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(-.5 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(-.5 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
		 graph save RoboVeht, replace
	 graph export "Figure6.png", as(png) width(1000) replace
	 
	 
	 	 	 	 	  	 foreach v of varlist Extorsiont {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Extortions per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Extortions", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(.3 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(.3 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 	 graph save Extorsiont, replace
	 graph export "Figure7.png", as(png) width(1000) replace
	 
	 	 	 	 	 	  
						  foreach v of varlist AbusoConft {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Embezzlement per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Embezzlement", pos(12))  ///
					xline(0.5, lwidth(8) lc(gs15) lpattern(solid))  ///
					text(.75 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(.75 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	 	 graph save AbusoConft, replace
	 graph export "Figure8.png", as(png) width(1000) replace
	 
	 
	 
	 	 	 	 	 	 	  	 foreach v of varlist Propertyt {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob], timevar(event) hdfe absorb(i.Clave)  cluster(Clave) ci(rline) ///
				graph_op(  xlabel(-9(2)33) ylabel(, angle(90)) xline(2, lcolor(black) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Vandalism per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("Vandalism", pos(12))  ///
					xline(0.5, lwidth(7) lc(gs15) lpattern(solid))  ///
					text(1 7.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(1 3.1 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
		 graph save Propertyt, replace
	 graph export "Figure9.png", as(png) width(1000) replace
	 
	 grc1leg Robberyt Theftt RoboCasat RoboVeht, cols(2) iscale(.5) legendfrom(Robberyt)
	 graph export "Combine_Robo.png", as(png) width(1500) height(1000) replace

	 grc1leg Extorsiont Propertyt, cols(2) iscale(.5) legendfrom(Extorsiont)
	 graph export "Combine_OC.png", as(png) width(1500) height(1000) replace
	 
/*	
	 foreach v of varlist her_tot_rate muertos_tot_rate{
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob],  hdfe absorb(i.ent_num) timevar(event) cluster(ent_num) ci(rline) ///
				graph_op(  xlabel(-9(1)9) ylabel( , angle(90)) xline(2, lcolor(red) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Rate per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("`label'", pos(12))  ///
					xline(.5, lwidth(12) lc(gs15) lpattern(solid)))  
					
	 }
	
		
	grc1leg her_tot_rate muertos_tot_rate  ///
	, iscale(.85) col(2)    imargin(2 2 2 2 ) name(FirstTime, replace)  
	graph display FirstTime, xsize(2) ysize(1)
	pause
	graph export "${output}Figure3.png", as(png) width(950) replace
	
	
		  	 foreach v of varlist fatal {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob],  hdfe absorb(i.ent_num) timevar(event) cluster(ent_num) ci(rline) ///
				graph_op(  xlabel(-9(1)9) ylabel( , angle(90)) xline(2, lcolor(red) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Proportion of accidents" )  /*
				*/ name(`v' , replace) title("`label'", pos(12))  ///
					xline(.5, lwidth(15) lc(gs15) lpattern(solid))  ///
					text(.0085 5.25 "Stay-at-home" "order ends" , j(left) size(small) )   ///
					text(.0085 3 "{&larr}", j(right) size(medium) angle(horizontal))  )   
	 }
	graph export "Figure4.png", as(png) width(1000) replace
	
	
 	 foreach v in $dep2  {
	 local label : variable label `v'  
	 eventdd  `v' i.mes i.yr  [aw=pob],  hdfe absorb(i.ent_num) timevar(event) cluster(ent_num) ci(rline) ///
				graph_op(  xlabel(-9(1)9) ylabel( , angle(90)) xline(2, lcolor(red) lpattern(solid)) /*change month 
				*/ xtitle("Months since the lockdown") ytitle( "Rate per 100,000 inhabitants" )  /*
				*/ name(`v' , replace) title("`label'", pos(12))  ///
					xline(.5, lwidth(10) lc(gs15) lpattern(solid))   ) 
	 }
	
	grc1leg $dep2   ///
	, iscale(.5) col(2)    imargin(2 2 2 2 ) name(FirstTime, replace)  
	graph display FirstTime, xsize(6) ysize(8)
	pause
	graph export "${output}Figure5.png", as(png) width(1000) replace
	
*/
*clear all
